/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import java.io.File;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;

/**
 * 二维码Controller
 * @author Jakemanse
 * @version 2013-05-27
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/qrCode")
public class QrCodeController extends BaseController {

	@Autowired
	private QRCodeService qrCodeService;
	
	@ModelAttribute
	public QRCode get(@RequestParam(required=false) Long id) {
		if (id != null){
			return qrCodeService.get(id);
		}else{
			return new QRCode();
		}
	}
	
	@RequiresPermissions("momarketing:qrCode:view")
	@RequestMapping(value = {"list", ""})
	public String list(QRCode qrCode, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<QRCode> page = qrCodeService.find(new Page<QRCode>(request, response), qrCode); 
        model.addAttribute("page", page);
		return "modules/momarketing/qrCodeList";
	}

	@RequiresPermissions("momarketing:qrCode:view")
	@RequestMapping(value = "form")
	public String form(QRCode qrCode, Model model) {
		model.addAttribute("qrCode", qrCode);
		return "modules/momarketing/qrCodeForm";
	}

	@RequiresPermissions("momarketing:qrCode:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,QRCode qrCode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, qrCode)){
			return form(qrCode, model);
		}
		qrCodeService.save(qrCode);
		addMessage(redirectAttributes, "保存二维码'" + qrCode.getId() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/momarketing/qrCode/?repage";
	}
	
	@RequiresPermissions("momarketing:qrCode:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		qrCodeService.delete(id);
		addMessage(redirectAttributes, "删除二维码成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/momarketing/qrCode/?repage";
	}
	
	
	
	@RequestMapping(value = "beautify"+"/{id}"+Global.URL_SUFFIX)
	public String beautify(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		//qrCodeService.delete(id); ;[
		QRCode qrCode = qrCodeService.get(id);
		request.setAttribute("imgurl", qrCode.getContent());
		request.setAttribute("qrCode", qrCode);
		//addMessage(redirectAttributes, "删除二维码成功");
		return "modules/qrcode/qrcodeBeautify";
	}
	
	@RequestMapping(value = "uploadicon"+"/{id}"+Global.URL_SUFFIX,method = RequestMethod.POST)
	public String addIcon(@PathVariable Long id ,  HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		//qrCodeService.delete(id); ;
		
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
		 multipartRequest.getFileMap();
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); 
		 String path =  request.getRealPath("userfiles");
		 File file = new File(path +File.separator+"icons"+File.separator +id+".jpg");
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { 
			 MultipartFile mf = entity.getValue();    
             String fileName = mf.getOriginalFilename();  
             try {
            	 mf.transferTo(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }
		 
		//addMessage(redirectAttributes, "删除二维码成功");
		return "modules/qrcode/qrcodeBeautify";
	}
	
	
	@RequestMapping(value = "urlqrcode",method = RequestMethod.POST)
	public String createURLImg( HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes) {
		try {
			String viewUrl = request.getParameter("viewUrl");
			String id = request.getParameter("id");
			QRCode qrCode = qrCodeService.get(Long.parseLong(id));
			File file = new File(qrCode.getJpegPath()+".jpeg");
			QRCodeUtils.downloadImg(viewUrl, file);
			PrintWriter out = response.getWriter();
			out.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//addMessage(redirectAttributes, "删除二维码成功");
		return "modules/qrcode/qrcodeBeautify";
	}
	
	
	
	
	
	
	
	

}
